package com.eianiao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eianiao.bean.Employee;
import com.eianiao.common.PageParam;
import com.eianiao.dao.EmployeeDao;
import com.eianiao.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

/**
 * @Author eianiao
 * @Date 2022 06
 * @Version 1.0
 **/
@Transactional
@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeDao employeeDao;

    /**
     * 登录员工界面
     * @param employee
     * @return
     */
    @Override
    public Employee login(Employee employee) {
        LambdaQueryWrapper<Employee> qw = new LambdaQueryWrapper<>();
        qw.eq(Employee::getUsername,employee.getUsername());
//        qw.eq(Employee::getPassword,employee.getPassword());
        String pwd = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        qw.eq(Employee::getPassword,pwd);
        return employeeDao.selectOne(qw);
    }

    /**
     * 添加员工信息
     * @param employee
     * @return
     */
    @Override
    public int add(Employee employee) {

        return employeeDao.insert(employee);
    }

    /**
     * 更新员工信息
     * @param employee
     * @return
     */
    @Override
    public int update(Employee employee) {

        return employeeDao.updateById(employee);
    }

    /**
     * 分页查询员工信息
     *
     * @param pageParam
     * @return
     */
    @Override
    public IPage findPage(PageParam pageParam) {
        IPage<Employee> employeeIPage = new Page<>(pageParam.getPage(), pageParam.getPageSize());
        LambdaQueryWrapper<Employee> qw = new LambdaQueryWrapper<>();
        qw.like(pageParam.getName() != null,Employee::getName,pageParam.getName());

        return employeeDao.selectPage(employeeIPage, qw);
    }
}
